{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# sklearn提供三个朴素贝叶斯分类算法\n",
    "\n",
    "\n",
    "## 1. 高斯朴素贝叶斯（GaussinaNB)\n",
    "\n",
    "特征变量时连续变量， 符合高斯分布\n",
    "\n",
    "## 2. 多项式朴素贝叶斯（MultinomialNB)\n",
    "\n",
    "特征是离散变量， 符合多项分布，在文档分类中特征变量体现在一个单词出现的次数或者是单词的TF-IDF值等\n",
    "\n",
    "## 3. 伯努利朴素贝叶斯（BernoulliNB)\n",
    "\n",
    "特征变量是布尔变量， 符合0/1分布， 在文档分类特征是单词是否出现\n",
    "\n",
    "伯努利朴素贝叶斯是以文件为粒度， 如果该单词在某文件中出现了即为1，否则为0.而多项式朴素贝叶斯是以单词为粒度， 会计算在某个文件中具体次数。而高斯朴素贝叶斯适合处理特征变量是离散变量，且符合正态分布（高斯分布）的情况。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "不重复的词： ['and', 'bayes', 'document', 'is', 'one', 'second', 'the', 'third', 'this']\n"
     ]
    }
   ],
   "source": [
    "from sklearn.feature_extraction.text import TfidfVectorizer\n",
    "tfidf_vec = TfidfVectorizer()\n",
    "\n",
    "documents = [\n",
    "    'this is the bayes document',\n",
    "    'this is the second second document',\n",
    "    'and the third one',\n",
    "    'is this the document'\n",
    "]\n",
    "\n",
    "tfidf_matrix = tfidf_vec.fit_transform(documents)\n",
    "\n",
    "print('不重复的词：', tfidf_vec.get_feature_names())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "每个单词的ID: {'this': 8, 'is': 3, 'the': 6, 'bayes': 1, 'document': 2, 'second': 5, 'and': 0, 'third': 7, 'one': 4}\n"
     ]
    }
   ],
   "source": [
    "# 输出每个单词对应的id值\n",
    "\n",
    "print('每个单词的ID:', tfidf_vec.vocabulary_)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "每个单词的tfidf值： [[0.         0.63314609 0.40412895 0.40412895 0.         0.\n",
      "  0.33040189 0.         0.40412895]\n",
      " [0.         0.         0.27230147 0.27230147 0.         0.85322574\n",
      "  0.22262429 0.         0.27230147]\n",
      " [0.55280532 0.         0.         0.         0.55280532 0.\n",
      "  0.28847675 0.55280532 0.        ]\n",
      " [0.         0.         0.52210862 0.52210862 0.         0.\n",
      "  0.42685801 0.         0.52210862]]\n"
     ]
    }
   ],
   "source": [
    "# 输出每个单词在每个文档中的TF-IDF值，向量里的顺序是按照词语的id顺序来的\n",
    "print('每个单词的tfidf值：', tfidf_matrix.toarray())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
